package com.cxyd.modules.wms.controller;

import com.cxyd.common.Utils.ExcelUtils;
import com.cxyd.common.page.PageData;
import com.cxyd.common.utils.Result;
import com.cxyd.common.annotation.LogOperation;
import com.cxyd.common.validator.AssertUtils;
import com.cxyd.common.validator.ValidatorUtils;
import com.cxyd.common.validator.group.AddGroup;
import com.cxyd.common.validator.group.DefaultGroup;
import com.cxyd.common.validator.group.UpdateGroup;
import com.cxyd.modules.wms.dto.ContainerDTO;
import com.cxyd.modules.wms.dto.ContainervwDTO;
import com.cxyd.modules.wms.dto.OrderReelDTO;
import com.cxyd.modules.wms.excel.ContainerExcel;
import com.cxyd.modules.wms.excel.OrderReelExcel;
import com.cxyd.modules.wms.service.ContainerDetailService;
import com.cxyd.modules.wms.service.ContainerService;
import com.cxyd.modules.wms.service.ContainervwService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 容器信息
 *
 * @author g_Cycle gengruyi01@163.com
 * @since 1.0.0 2023-04-27
 */
@RestController
@RequestMapping("wms/container")
public class ContainerController {
    private final ContainerService containerService;
    private final ContainervwService containervwService;
    private final ContainerDetailService containerDetailService;

    public ContainerController(ContainerService containerService, ContainervwService containervwService, ContainerDetailService containerDetailService) {
        this.containerService = containerService;
        this.containervwService = containervwService;
        this.containerDetailService = containerDetailService;
    }

//    @GetMapping("page")
//    @RequiresPermissions("wms:container:page")
//    public Result<PageData<ContainerDTO>> page(@RequestParam Map<String, Object> params){
//        PageData<ContainerDTO> page = containerService.page(params);
//
//        return new Result<PageData<ContainerDTO>>().ok(page);
//    }

    @GetMapping("page")
    @RequiresPermissions("wms:container:page")
    public Result<PageData<ContainervwDTO>> page(@RequestParam Map<String, Object> params){
        PageData<ContainervwDTO> page = containervwService.page(params);

        return new Result<PageData<ContainervwDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @RequiresPermissions("wms:container:info")
    public Result<ContainerDTO> get(@PathVariable("id") Long id){
        ContainerDTO data = containerService.get(id);

        return new Result<ContainerDTO>().ok(data);
    }

    @PostMapping
    @LogOperation("保存")
    @RequiresPermissions("wms:container:save")
    public Result save(@RequestBody ContainerDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        containerService.save(dto);

        return new Result();
    }

    @PutMapping
    @LogOperation("修改")
    @RequiresPermissions("wms:container:update")
    public Result update(@RequestBody ContainerDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        containerService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @LogOperation("删除")
    @RequiresPermissions("wms:container:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        containerService.delete(ids);

        return new Result();
    }


    @GetMapping("contLocate/{contNo}")
    //@RequiresPermissions("wms:task:reelid")
    @LogOperation("货格号请求")
    public Result contLocateAsk(@PathVariable("contNo") String contNo) {
        String contLocateNo = containerDetailService.contLocateAsk(contNo);
        if(contLocateNo==null||contLocateNo.isEmpty()){
            return new Result().error("料箱["+contNo+"]无可用空货格号");
        }
        return new Result().success(contLocateNo);
    }

    @GetMapping("export")
    public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<ContainervwDTO> list = containervwService.getList(params);
        ExcelUtils.exportExcelToTarget(response, null, list, ContainerExcel.class);
    }
}